---@class WenheYanbing.PlayerActionHandler
local PlayerActionHandler = class("WenheYanbing.PlayerActionHandler")

function PlayerActionHandler:handle(room, playerId, data)
    local player = room:getPlayerById(playerId)
    if not player then return false end
    
    local action = data.action
    local actionData = data.data or {}
    
    local success = false
    local result = {}
    
    if action == "buy" then
        success = player:buyMinion(actionData.tavernIndex)
        if success then
            result.tavernMinions = player.tavernMinions
            result.gold = player.gold
            result.hand = player.hand
        end
    elseif action == "refresh" then
        success = player:refreshTavern()
        if success then
            result.tavernMinions = player.tavernMinions
            result.gold = player.gold
        end
    elseif action == "levelup" then
        success = player:upgradeTavern()
        if success then
            result.tavernTier = player.tavernTier
            result.gold = player.gold
            result.upgradeCost = player:getUpgradeCost()
        end
    elseif action == "freeze" then
        success = player:freezeTavern()
        if success then
            result.freezeTavern = player.freezeTavern
        end
    elseif action == "place" then
        success = player:placeMinion(actionData.handIndex, actionData.boardPosition)
        if success then
            result.hand = player.hand
            result.board = player.board
        end
    elseif action == "sell" then
        success = player:sellMinion(actionData.boardPosition)
        if success then
            result.board = player.board
            result.gold = player.gold
        end
    end
    
    -- 发送更新给玩家
    if success then
        room:doNotify(player, "WenheYanbing.PlayerAction", {
            success = true,
            action = action,
            result = result
        })
    else
        room:doNotify(player, "WenheYanbing.PlayerAction", {
            success = false,
            action = action,
            error = "操作失败"
        })
    end
    
    return success
end

return PlayerActionHandler